package com.GLM.Viewer.Party;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.GLM.Constants.Constant;
import com.GLM.Entity.MeetDataEntity;
import com.GLM.Tools.DB.DAO.UserInfoDAO;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Viewer.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class CreateNewPartyTimeViewActivity extends Activity implements OnClickListener{
	Button goBackButton;
	Button topSaveButton;
	private String mPartyBeginTime;
	private Calendar calendar;
	private String parentActivity;
	private MeetDataEntity mParty;
	private int meet_mg_id;
	
	private TextView mTimeView;
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_newparty_step3);
		Intent intent = getIntent();
		if(intent != null){
			Bundle bundle = intent.getExtras();
			if(bundle != null){
				parentActivity = bundle.getString("parentIntent");
				mParty = (MeetDataEntity) bundle.getSerializable("party");
				meet_mg_id = bundle.getInt("meet_mg_id");
			}
		}
		mTimeView = (TextView)findViewById(R.id.newparty_time_selected);
		goBackButton = (Button)findViewById(R.id.goback_button_time);
		goBackButton.setOnClickListener(this);
		topSaveButton = (Button)findViewById(R.id.ftop_save_button);
		topSaveButton.setOnClickListener(this);
		DatePicker dp = (DatePicker) findViewById(R.id.newparty_datepicker);
		TimePicker tp = (TimePicker)findViewById(R.id.newparty_timepicker);
		tp.setIs24HourView(true);
		
		calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		dp.init(year, month, day, new DatePicker.OnDateChangedListener() {
			
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// TODO Auto-generated method stub
				calendar.set(Calendar.YEAR, year);
				calendar.set(Calendar.MONTH, monthOfYear);
				calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
				
				mPartyBeginTime = format.format(calendar.getTime());
			    mTimeView.setText(mPartyBeginTime);
			}
		});
		
		tp.setOnTimeChangedListener(new OnTimeChangedListener() {
			
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				// TODO Auto-generated method stub
				calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
				calendar.set(Calendar.MINUTE, minute);
				mPartyBeginTime = format.format(calendar.getTime());
			    mTimeView.setText(mPartyBeginTime);
			}
		});
	}
	


	@SuppressWarnings("static-access")
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == topSaveButton){
			mPartyBeginTime = format.format(calendar.getTime());
		    Intent intent;
		    boolean buildparty = false;
		    if(parentActivity != null && parentActivity.equals("PartyDetailActivity")){
		    	buildparty = true;
		    	intent = new Intent(CreateNewPartyTimeViewActivity.this,PartyDetailActivity.class);
		    }
		    else {
		    	intent = new Intent(CreateNewPartyTimeViewActivity.this, CreateNewPartyViewActivity.class);
		    	intent.putExtra("mPartyBeginTime", mPartyBeginTime);
		    }
		    if(buildparty){
		    	int status = initReBuildParty();
		    	intent.putExtra("status", status);
		    }
		    setResult(RESULT_OK, intent);
		    finish();
		}
		else if(v == goBackButton){
			finish();
		}
	}

	
	public int  initReBuildParty(){
		JSONObject jsonData = new JSONObject();
		JSONResponse jsonResponse = null;
		int status = -1;
		try {
			jsonData.put(Constant.PARAM_USERID, UserInfoDAO.getUserInfo(CreateNewPartyTimeViewActivity.this).getUserId());
			jsonData.put("meet_mg_id", meet_mg_id);
			jsonData.put("meet_content", mParty.getMeet_content());
			jsonData.put("meet_name",mParty.getMeet_name());
			jsonData.put("meet_beginDate", mPartyBeginTime);
			jsonData.put("meet_endDate", mPartyBeginTime);
			JSONArray ja = new JSONArray();
			int[] ids = mParty.getMeet_enlist();
			for(int i=0; i<ids.length; i++){
				ja.put(ids[i]);
			}
			jsonData.put("enlist", ja);
			jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_CREATENEWPARTY , jsonData);
			
			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				
				status = data.getInt(Constant.RESP_STATUS);
				
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return status;
	}
	
}
